﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BrokerBase.Lib.Product;
using BrokerBase.Lib.Tool;
using Broker.View.Comm;
using Telerik.Windows.Controls;
using BrokerBase.Lib.Policy;

namespace Broker.View.ProductView
{
    /// <summary>
    /// ResponsibilityForm.xaml 的交互逻辑
    /// </summary>
    public partial class DutyForm 
    {
        private IDuty duty;
        private IDuty original;

        public DutyForm(IDuty duty)
        {

            try
            {
                InitializeComponent();

                original = duty;

                this.duty = new Duty(original.Owner);

                try
                {
                    BaseTools.CopyTo<IDuty>(original, this.duty);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误提示");
                }

                Loaded += new RoutedEventHandler(DutyForm_Loaded);
                PreviewClosed += new EventHandler<WindowPreviewClosedEventArgs>(DutyForm_PreviewClosed);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示");
            }
        }

        public bool IsCommit { get; set; }

        public Action<IDuty> CallBack { get; set; }

        void DutyForm_PreviewClosed(object sender, WindowPreviewClosedEventArgs e)
        {
            if (IsCommit)
            {
                if (Verify())
                {
                    BaseTools.CopyTo<IDuty>(duty, original);
                    if (CallBack != null)
                        CallBack(original);
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        void DutyForm_Loaded(object sender, RoutedEventArgs e)
        {
            DataContext = duty;

            if (LibGlobal.AppType == AppType.Business)
            {
                btnConfirm.Visibility = Visibility.Collapsed;
                btnCopy.Visibility = Visibility.Collapsed;
                btnTemplate.Visibility = Visibility.Collapsed;
                btnTest.Visibility = Visibility.Collapsed;

                tabCode.Visibility = Visibility.Hidden;

            }
        }

        private void btnTemplate_Click(object sender, RoutedEventArgs e)
        {
            IList<IDuty> duties = ProductUtil.LoadDutyTemplate();

            ListSelForm frm = new ListSelForm();

            GridViewDataColumn col1 = new GridViewDataColumn();

            col1.Header = "名称";
            col1.Width = new GridViewLength(160, GridViewLengthUnitType.Auto);
            col1.DataMemberBinding = new Binding("Name");

            GridViewDataColumn col2 = new GridViewDataColumn();
            col2.Header = "描述";
            col2.Width = new GridViewLength(200, GridViewLengthUnitType.Star);
            col2.DataMemberBinding = new Binding("Description");

            frm.GridView.Columns.Add(col1);
            frm.GridView.Columns.Add(col2);

            IDuty d = frm.SelectByList<IDuty>(duties);

            if (d != null)
            {
                duty.Name = d.Name;
                duty.HolderStates = d.HolderStates;
                duty.InsurantStates = d.InsurantStates;
                duty.Description = d.Description;
                duty.Code = d.Code;

                DataContext = null;
                DataContext = duty;
            }

        }

        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            IsCommit = true;
            Close();
        }

        private bool Verify()
        {
            if (string.IsNullOrEmpty(duty.Name))
            {
                MessageBox.Show("请输入名称", "提示");
                return false;
            }

            if (!string.IsNullOrEmpty(duty.Code))
            {
                if (!Test())
                {
                    MessageBox.Show("表达式编译发生错误!", "错误提示");
                }
            }

            //判断对象是否已经存在duties中
            IList<IDuty> ds=duty.Owner.Duties;

            if(ds.FirstOrDefault(p=>p.Id==duty.Id)!=null)
            {
                if(ds.Count(d=>d.Name==duty.Name)>1)
                {
                    MessageBox.Show(string.Format("名称[{0}]已经存在",duty.Name),"提示");
                    return false;
                }
            }else
            {
                if(ds.Count(d=>d.Name==duty.Name)>0)
                {
                    MessageBox.Show(string.Format("名称[{0}]已经存在",duty.Name),"提示");
                    return false;
                }
            }

            return true;
        }

        private void btnTest_Click(object sender, RoutedEventArgs e)
        {

            Test();
        }

        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {
            ProductListPage p = new ProductListPage();
            p.PageModel = ListPageModel.List;

            RadWindow w = new RadWindow();
            w.Owner = this;
            w.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            w.Height = 500;
            w.Width = 700;

            Frame f = new Frame();

            f.Content = p;
            w.Content = f;
            w.Show();
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            IsCommit = false;
            Close();
        }

        private bool Test()
        {
            IList<IDuty> duies = new List<IDuty>();
            duies.Add(duty);

            bool succes = false;
            string info = null;

            IList<Action<IPolicyPeriodActing, IDutyActing>> ls = ProductUtil.CreateDutyAction(duies, out succes, out info);
            txtCompileInfo.Text = info;

            return succes;
        }

        private void txtDescription_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
        
    }
}
